<?php
/**
 * TopAdmin
 * 版权所有 2018-2027 TopAdmin，并保留所有权利。
 * Author: TopAdmin
 * Date: 2021/2/27
 */
namespace app\admin\controller\member;

use app\common\controller\Backend;
use app\common\model\UserGroup;
use app\common\traits\Curd;
use think\facade\Request;
use think\facade\View;
use app\common\model\UserLevel;
use app\common\model\User as UserModel;
use think\facade\Db;

class User extends Backend{
    use Curd;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->modelClass = new UserModel();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $list =  $this->modelClass->alias('u')
                ->where($where)
                ->join('userLevel ul','u.level_id = ul.id') //此处表别名不能加as
                ->join('userGroup ug','u.group_id = ug.id') //此处表别名不能加as
                ->field('ul.name as levelName,u.*')
                ->field('ug.name as groupName,u.*')
                ->order('u.id desc,u.create_time desc')
                ->page($page, $limit)
                ->select();
            $total = $this->modelClass->where($where)->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        }
        return view();
    }


    public function add(){
        if (Request::isPost()) {
            $data = $this->request->post();
            try{
                $this->validate($data, 'User');
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $model = new UserModel();
            $res =$model->add($data);
            if ($res) {
                $this->success(lang('add success'),url('index'));
            } else {
                $this->error(lang('add fail'));
            }
        }
        $userLevel = UserLevel::where('status',1)->select();
        $userGroup = UserGroup::where('status',1)->select();
        $info['avatar']  = '';
        $view = [
            'info'  =>'',
            'title' => lang('add'),
            'userLevel'=>$userLevel,
            'userGroup'=>$userGroup,
        ];
        View::assign($view);
        return view();
    }

    public function edit(){
        if (Request::isPost()) {
            $data = $this->request->post();
            try {
                $this->validate($data, 'User');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $model = new UserModel();
            $res =$model->edit($data);
            if ($res) {
                $this->success(lang('edit success'), url('index'));
            } else {
                $this->error(lang('edit fail'));
            }
        }
        $info = UserModel::find(Request::get('id'));
        $userLevel = UserLevel::where('status',1)->select();
        $userGroup = UserGroup::where('status',1)->select();
        $view = [
            'info' => $info,
            'title' => '修改',
            'userLevel'=>$userLevel,
            'userGroup'=>$userGroup,
        ];
        View::assign($view);
        return view('add');

    }


}